# -*-coding:utf-8-*-

import pyrestful.rest
from pyrestful import mediatypes
from pyrestful.rest import get, post, put, delete
from pymongo.collection import ReturnDocument
from utils.auth import authenticated
import time, json
from datetime import datetime, timedelta
from handlers.base import BaseHandler
from tornado.gen import coroutine


class JobsHandler(BaseHandler):
    def __init__(self, application, request, **kwargs):
        super(JobsHandler, self).__init__(application, request, **kwargs)
        # self.jobs_dao = self.settings['daos']['jobs']
        self.jobs_dao = self.settings['daos_async']['jobs']

    def check_permissions(self, permissions):
        return True

    # @authenticated
    # @get(_path='/platform/api/jobs', _produces=mediatypes.APPLICATION_JSON)
    # def get_job_list(self):
    #     result = self.jobs_dao.get_job_list(self.user_id)
    #     self.response_success(result)

    @coroutine
    @authenticated
    @post(_path='/platform/api/jobs', _produces=mediatypes.APPLICATION_JSON)
    def create_job(self):
        data = json.loads(self.request.body)
        result = yield self.jobs_dao.create_job(self.user_id, data)
        self.respond_success(result)

    @coroutine
    @authenticated
    @get(_path='/platform/api/jobs/{jid}', _type=[str], _produces=mediatypes.APPLICATION_JSON)
    def get_job(self, jid):
        result = yield self.jobs_dao.get_job(self.user_id, jid)
        self.respond_success(result)

    @coroutine
    @authenticated
    @delete(_path='/platform/api/jobs/{jid}', _types=[str], _produces=mediatypes.APPLICATION_JSON)
    def delete_job(self, jid):
        result = yield self.jobs_dao.delete_job(self.user_id, jid)
        self.respond_success(result)
